//
//  NIStarView.m
//  gweAD
//
//  Created by Navinfo on 2017/10/17.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NIStarView.h"

@implementation NIStarView{
    
    __weak IBOutlet UIView *contentView;
    
    __weak IBOutlet NSLayoutConstraint *starHeightConstraint;
    
    __weak IBOutlet NSLayoutConstraint *starWidthConstraint;
    
    CGFloat starScale;
    
    
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.score = 1;
    }
    return self;
}

- (void)setStarImageScale:(CGFloat)scale{
    if (starScale == scale) {
        return;
    }
    starScale = scale;
    starHeightConstraint.constant = starHeightConstraint.constant * starScale;
    starWidthConstraint.constant = starWidthConstraint.constant * starScale;
    [contentView setNeedsLayout];
    [contentView layoutIfNeeded];
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = [touches anyObject];
//    CGPoint point = [touch locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [self setStarForegroundViewWithPoint:point];
    return;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [self setStarForegroundViewWithPoint:point];
    return;
}

-(void)setStarForegroundViewWithPoint:(CGPoint)point{
    if (!_enable) {
        return;
    }
//    CGFloat star0X = [contentView viewWithTag:100].frame.origin.x + ((UIImageView *)[contentView viewWithTag:100]).image.size.width/2;
    CGFloat star1X = [contentView viewWithTag:101].frame.origin.x + ((UIImageView *)[contentView viewWithTag:101]).image.size.width/3;
    CGFloat star2X = [contentView viewWithTag:102].frame.origin.x + ((UIImageView *)[contentView viewWithTag:102]).image.size.width/3;
    CGFloat star3X = [contentView viewWithTag:103].frame.origin.x + ((UIImageView *)[contentView viewWithTag:103]).image.size.width/3;
    CGFloat star4X = [contentView viewWithTag:104].frame.origin.x + ((UIImageView *)[contentView viewWithTag:104]).image.size.width/3;
    
    
    if(point.x >= star4X){
        self.score = 5;
    }else if(point.x >= star3X){
        self.score = 4;
    }else if(point.x >= star2X){
        self.score = 3;
    }else if(point.x >= star1X){
        self.score = 2;
    }else{
        self.score = 1;
    }
    
    [self.delegate onStarValueDidChange];
    
}

- (void)setScore:(NSInteger)score{
    _score = score;
    if (!_enable) {
        [contentView viewWithTag:100].hidden = YES;
        [contentView viewWithTag:101].hidden = YES;
        [contentView viewWithTag:102].hidden = YES;
        [contentView viewWithTag:103].hidden = YES;
        [contentView viewWithTag:104].hidden = YES;
    }
    ((UIImageView *)[contentView viewWithTag:100]).hidden = NO;
    ((UIImageView *)[contentView viewWithTag:100]).image = [UIImage imageNamed:@"icon_star_light"];
    
    if(score > 1){
        ((UIImageView *)[contentView viewWithTag:101]).hidden = NO;
        ((UIImageView *)[contentView viewWithTag:101]).image = [UIImage imageNamed:@"icon_star_light"];
    }else{
        ((UIImageView *)[contentView viewWithTag:101]).image = [UIImage imageNamed:@"icon_star"];
    }
    
    
    if(score > 2){
        ((UIImageView *)[contentView viewWithTag:102]).hidden = NO;
        ((UIImageView *)[contentView viewWithTag:102]).image = [UIImage imageNamed:@"icon_star_light"];
    }else{
        ((UIImageView *)[contentView viewWithTag:102]).image = [UIImage imageNamed:@"icon_star"];
    }
    
    if(score > 3){
        ((UIImageView *)[contentView viewWithTag:103]).hidden = NO;
        ((UIImageView *)[contentView viewWithTag:103]).image = [UIImage imageNamed:@"icon_star_light"];
    }else{
        ((UIImageView *)[contentView viewWithTag:103]).image = [UIImage imageNamed:@"icon_star"];
    }
    
    if(score > 4){
        ((UIImageView *)[contentView viewWithTag:104]).hidden = NO;
        ((UIImageView *)[contentView viewWithTag:104]).image = [UIImage imageNamed:@"icon_star_light"];
    }else{
        ((UIImageView *)[contentView viewWithTag:104]).image = [UIImage imageNamed:@"icon_star"];
    }
}

- (void)setScoreFloat:(CGFloat)scoreFloat{
    ((UIImageView *)[contentView viewWithTag:100]).image = [self getImage:(scoreFloat - 0)];
    ((UIImageView *)[contentView viewWithTag:101]).image = [self getImage:(scoreFloat - 1)];
    ((UIImageView *)[contentView viewWithTag:102]).image = [self getImage:(scoreFloat - 2)];
    ((UIImageView *)[contentView viewWithTag:103]).image = [self getImage:(scoreFloat - 3)];
    ((UIImageView *)[contentView viewWithTag:104]).image = [self getImage:(scoreFloat - 4)];
}



- (UIImage *)getImage:(CGFloat)percent{
    UIImage *starLightImage = [UIImage imageNamed:@"icon_star_light"];
    UIImage *starImage;
    if(_lightGray){
        starImage = [UIImage imageNamed:@"icon_star2"];
    }else{
        starImage = [UIImage imageNamed:@"icon_star3"];
    }
    
    
    if (percent >= 1) {
        return starLightImage;
    }else if (percent <= 0){
        return starImage;
    }
   
    CGRect rect = CGRectMake(0, 0, starLightImage.size.width*[UIScreen mainScreen].scale * percent, starLightImage.size.height*[UIScreen mainScreen].scale);
    CGImageRef imageRef=CGImageCreateWithImageInRect([starLightImage CGImage],rect);
    UIImage *newStarLightImage=[UIImage imageWithCGImage:imageRef];
    
    UIGraphicsBeginImageContextWithOptions(starLightImage.size,NO,[UIScreen mainScreen].scale);
    [starImage drawInRect:CGRectMake(0, 0, starLightImage.size.width,starLightImage.size.height)];
    [newStarLightImage drawInRect:CGRectMake(0, 0, starLightImage.size.width*percent,starLightImage.size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}



@end
